﻿XXXX.point = {
	map : null,
    loadMap: false,
	count : 0,
	init : function() {
        if(!!XXXX.point.map) XXXX.point.map=null; // release memory

		var tab = Ext.getCmp('tabView');
		
		var panel = new Ext.Panel( {
			renderTo : 'form_point',
			id : 'map_point',
			layout: 'border',
			height : tab.getInnerHeight(),
			width : tab.getInnerWidth(),
			defaults: {
		        collapsible: true, 
		        //bodyStyle: 'padding:5px',
				split: true
		    },
		    items:[  
				{
					title: "地图展示",
					region:"south",  
					height:tab.getInnerHeight()/2+150,  
					html: '<div id="map_pt" style="overflow:auto;width:100%;height:100%"></div>'
				},  
				{
					title: "运单列表",
					id: 'pt_form',
					region:"center",  
					xtype: "form",
					labelWidth :75,
					frame :true,
					bodyStyle :'padding:5px 5px 0',
					width :300,
					defaults : {
						width :330
					},
					defaultType :'textfield',
					items : [
						{
							fieldLabel : '起点',
							id : 'pt_start',
							value : '116.329701, 39.982983',
							allowBlank : false,
							blankText : '启点不能为空'
						},
						{
							fieldLabel : '终点',
							id : 'pt_end',
							value : '116.403578, 39.914837',
							allowBlank : false,
							blankText : '终点不能为空'
						},
		
					],
					buttons : [ {
						text :'寻路',
						handler : function() {
							var start = Ext.getCmp("pt_start").getValue();
							var end = Ext.getCmp("pt_end").getValue();
							
							var spts = start.split(",");
							var epts = end.split(",");
							
							XXXX.point.map.clearOverlays();
							
							var driving = new BMap.DrivingRoute(XXXX.point.map, {  
							 	renderOptions: {  
								   	map: XXXX.point.map,  
								   	autoViewport: true
								},
								onPolylinesSet: function(routes){
							   		if(!!routes){
							   			var r = routes[0];
							   			var pts = r.getPath();
							   			var str = "";
							   			Ext.each(pts, function(p) {
											str += p.lng+","+p.lat+"<br/>";
										}, this);
										
										Ext.getCmp("pt_points").update(str);
							   		}
							   	}
							});  
							driving.search(new BMap.Point(parseFloat(spts[0]), parseFloat(spts[1])), 
											new BMap.Point(parseFloat(epts[0]), parseFloat(epts[1])));
							
						}
					} ]
				  
				},
				{
					title: "位置列表",
					id: 'pt_points',
					region:"east",  
					autoScroll: true,
					width: 350,
					html: '<div id="pts" style="width:100%;height:100%"></div>',
				    frame: true
				}
			]  
		    
		});

        if(!XXXX.loadMap) {
            var script = document.createElement("script");
            script.src = "http://api.map.baidu.com/api?v=2.0&ak=WFVCR2Q3f7k9xRrlRqEjIKGy&callback=initMap";
            document.body.appendChild(script);
            XXXX.loadMap = true;
        }else{
            initMap();
        }
	}
}

function initMap(){

    var bmap = new BMap.Map("map_pt");
    XXXX.point.map = bmap;
    var point = new BMap.Point(116.404, 39.915);
    bmap.centerAndZoom(point,12);
    bmap.enableScrollWheelZoom();

	bmap.addEventListener("click", function(e){
		 var msg = e.point.lng + ", " + e.point.lat;
		 var flag = XXXX.point.count++ % 2;
		 var st = (flag == 0) ? "起点" : "终点";
		 Ext.MessageBox.alert(st + "坐标", msg);
		 if(flag == 0){
		 	Ext.getCmp("pt_start").setValue(msg);
		 }else{
		 	Ext.getCmp("pt_end").setValue(msg);
		 }
		 
		});
	
}

System.register("point", XXXX.point);